/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#ifndef FTWIDGETLIST_H
#define FTWIDGETLIST_H

#include <QtGui>

class FTPushButton;
class FTWidgetListItem;

enum FTWidgetListState { atRest, atScrolling, atMoving };

class FTWidgetList : public QWidget
{
    Q_OBJECT
public:
    explicit FTWidgetList(QWidget *parent = 0);

    // This event filter is used to "intercept" mouse events sent to children
    // widgets...
    bool eventFilter(QObject*, QEvent*);

    // Add a widget (with its identification key)
    void appendWidget( int, QWidget* );
    void appendWidget( QWidget* );
    void removeWidget( int );

    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void paintEvent(QPaintEvent *);
    void resizeEvent(QResizeEvent *);
    void timerEvent(QTimerEvent *);

    void setActions( bool move, bool del = false );
    void setAutoSelect(bool);
    bool autoSelect() const;

signals:
    void orderChanged( const QList<int>& );
    void itemDeleted(int);
    void selectedItem(int);

private slots:
    void dragHere(FTWidgetListItem*);

private:
    bool allowMove;
    bool allowDelete;
    bool _autoSelect;

    // Store all the items, with their keys.
    // We store them like this because we will need the keys from the item
    // and not the other way around...
    QHash<FTWidgetListItem*, int> items;
    QList<FTWidgetListItem*> items_order;

    // Mouse position when start scrolling
    QPoint scroll_start;
    // This timer is used to stop state change
    QBasicTimer state_change_timer;

    FTWidgetListItem* delete_widget;
    FTWidgetListItem* moving_widget;
    FTWidgetListItem* drop_target;
    FTWidgetListState state;

    FTWidgetListItem* getProperWidget( const QObject* );

    void performDragAndDrop();
    void resetStateMachine();
    void performScrolling();

};

#endif // FTWIDGETLIST_H
